package com.xinqi.modules.course.demonstrate.service.impl;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.modules.course.course.mapper.CourseMapper;
import com.xinqi.modules.course.demonstrate.service.DemoNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

/**
 * 示范课程
 *
 * @author dzy
 */

@Service("demoNoticeService")
@RequiredArgsConstructor
public class DemoNoticeServiceImpl implements DemoNoticeService {

    private final IdentifierGenerator identifierGenerator;
    private final CourseMapper courseMapper;

    /**
     * 生成公告数据SQL
     */

    @Override
    @Async("threadPoolTaskExecutor")
    public void initNoticeSql(Long userId, Long classId, Map<String, String> idsMap, Map<String, String> chapterMap) {
        StringBuilder sql = new StringBuilder();
        String discussSql = "INSERT INTO discuss.co_course_notice(`id`, `title`, `content`, `fileurls`, `videos`, `images`, `class_id`, `synchro`, `class_ids`, `is_confirm`, `now_release`, `timing`, `release_time`, `end_time`, `is_top`, `top_time`, `status`, `extra`, `create_by`, `create_time`, `update_by`, `update_time`, `is_delete`, `delete_time`) " +
            "VALUES (" + idsMap.get("1663469811618603009") + ", '第二次数学月考范围', '各位同学：\\n即将到来的数学月考将涉及以下章节的内容，请务必认真复习：\\n\\n一、 高中数学基础知识：\\n\\n （一）整式与分式\\n1、一次函数与二次函数\\n2、 指数与对数\\n3、 三角函数与解三角形\\n4、 平面几何与立体几何\\n\\n二、 高中数学选修课内容：\\n\\n（一）概率与统计\\n1、 数学分析\\n2、微积分。\\n\\n请同学们认真复习以上章节的相关知识点，做好准备，以便在月考中取得好成绩。\\n\\n祝愿各位同学考试成功！\\n\\n谢谢。', NULL, NULL, NULL, 1661184856792928257, 0, NULL, 0, 0, 0, '2023-05-30 20:13:55', '2023-06-20 00:00:00', 0, NULL, 1, NULL, 1645325062446964737, '2023-05-30 16:58:04', 1645325062446964737, '2023-05-31 11:47:39', 0, NULL)," +
            "(" + idsMap.get("1663706828562554882") + ", '关于端午节放假的通知', '尊敬的家长和同学们：\\n端午节即将到来，按照国家法定节假日规定放假三天，具体安排如下：\\n 1. 放假时间：2023年6月22日至6月24日。6月25 日按照规定正常上课。\\n 2. 放假期间，学校各项活动停止，校园内禁止进出人员，保障同学们的安全。\\n 3. 节日期间，同学们要注意安全，避免参加危险的活动，如游泳、攀岩等。 \\n 4. 放假后，同学们要按时返校，遵守学校的规定，做到文明礼仪，积极参加学校组织的各项活动。 \\n最后，祝愿同学们在端午节期间，健康快乐，度过一个愉快的假期！ \\n特此通知。 \\n学校：新奇中学 \\n日期：2023年6月3日 \\n请点击“确认”回复此公告', NULL, NULL, NULL, 1661184856792928257, 0, NULL, 1, 1, 0, '2023-05-31 08:39:53', '2023-06-20 09:00:00', 0, NULL, 1, NULL, 1645325062446964737, '2023-05-31 08:39:53', 1645325062446964737, '2023-05-31 11:47:33', 0, NULL);";
        // 更新章节
        Set<String> keyList = chapterMap.keySet();
        for (String item : keyList) {
            String value = chapterMap.get(item);
            discussSql = discussSql.replaceAll(item, value);
        }
        // 更新班级
        discussSql = discussSql.replaceAll("1661184856792928257", classId.toString());
        sql.append(discussSql);

        // 讨论学生日志SQL
        String discussLogSql = "INSERT INTO discuss.co_course_notice_student_log(`id`, `class_id`, `content_id`, `student_id`, `is_read`, `is_confirm`, `create_by`, `create_time`, `update_by`, `update_time`) " +
            "VALUES (" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1650458704827445249, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1650458704827445249, '2023-05-31 08:36:31')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1653986821394567169, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1653986821394567169, '2023-05-30 21:22:59')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1654693737893134337, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1654693737893134337, '2023-05-30 21:30:11')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1655410589569056769, 0, 0, 1645325062446964737, '2023-05-30 20:13:55', 1645325062446964737, '2023-05-30 20:13:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1655650372358701057, 0, 0, 1645325062446964737, '2023-05-30 20:13:55', 1645325062446964737, '2023-05-30 20:13:55')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1656548738307391489, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1656548738307391489, '2023-05-31 08:45:28')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1659403958154625026, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1659403958154625026, '2023-06-14 21:37:08')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663469811618603009, 1661185385489694721, 1, 0, 1645325062446964737, '2023-05-30 20:13:55', 1661185385489694721, '2023-05-31 09:04:35')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1650458704827445249, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1650458704827445249, '2023-05-31 09:09:25')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1653986821394567169, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1653986821394567169, '2023-05-31 08:43:32')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1654693737893134337, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1654693737893134337, '2023-05-31 08:42:16')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1655410589569056769, 0, 0, 1645325062446964737, '2023-05-31 08:39:53', 1645325062446964737, '2023-05-31 08:39:53')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1655650372358701057, 0, 0, 1645325062446964737, '2023-05-31 08:39:53', 1645325062446964737, '2023-05-31 08:39:53')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1656548738307391489, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1656548738307391489, '2023-05-31 08:44:53')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1659403958154625026, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1659403958154625026, '2023-06-14 21:37:06')," +
            "(" + identifierGenerator.nextId(userId) + ", 1661184856792928257, 1663706828562554882, 1661185385489694721, 1, 1, 1645325062446964737, '2023-05-31 08:39:53', 1661185385489694721, '2023-05-31 08:59:09');";
        // 更新内容ID
        Set<String> contentList = idsMap.keySet();
        for (String item : contentList) {
            String value = idsMap.get(item);
            discussLogSql = discussLogSql.replaceAll(item, value);
        }
        // 更新班级ID
        discussLogSql = discussLogSql.replaceAll("1661184856792928257", classId.toString());
        sql.append(discussLogSql);

        // 执行SQL
        courseMapper.insertAllSql(sql.toString());
    }

}
